<?php
require_once __DIR__ . '/classes/Logger.php';

$logger = new Logger();
$logContent = $logger->getTodayLog();

// 处理清空日志请求
if (isset($_POST['clear_log'])) {
    $logger->clearTodayLog();
    header('Location: view_log.php');
    exit;
}

// 处理刷新请求
$autoRefresh = isset($_GET['refresh']) && $_GET['refresh'] == '1';
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统日志 - 神马搜索关键词轮播系统</title>
    <?php if ($autoRefresh): ?>
    <meta http-equiv="refresh" content="10">
    <?php endif; ?>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: 'Courier New', monospace;
            background-color: #1e1e1e;
            color: #d4d4d4;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.3);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .header h1 {
            margin: 0;
            font-size: 24px;
        }
        
        .header-actions {
            display: flex;
            gap: 10px;
        }
        
        .btn {
            background: rgba(255,255,255,0.2);
            color: white;
            padding: 8px 15px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
            font-size: 14px;
        }
        
        .btn:hover {
            background: rgba(255,255,255,0.3);
        }
        
        .btn-danger {
            background: #dc3545;
        }
        
        .btn-danger:hover {
            background: #c82333;
        }
        
        .log-container {
            padding: 20px;
            max-width: 1400px;
            margin: 0 auto;
        }
        
        .log-info {
            background: #2d2d30;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .log-content {
            background: #1e1e1e;
            border: 1px solid #3e3e42;
            border-radius: 5px;
            padding: 20px;
            white-space: pre-wrap;
            word-wrap: break-word;
            font-size: 14px;
            line-height: 1.6;
            max-height: calc(100vh - 250px);
            overflow-y: auto;
        }
        
        .log-line {
            margin: 5px 0;
        }
        
        .log-line.info {
            color: #4ec9b0;
        }
        
        .log-line.error {
            color: #f48771;
        }
        
        .log-line.warning {
            color: #dcdcaa;
        }
        
        .log-line.debug {
            color: #9cdcfe;
        }
        
        .empty-log {
            text-align: center;
            padding: 50px;
            color: #858585;
            font-size: 16px;
        }
        
        .toggle-refresh {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .toggle-refresh input[type="checkbox"] {
            width: 20px;
            height: 20px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>📋 系统日志</h1>
        <div class="header-actions">
            <div class="toggle-refresh">
                <input type="checkbox" id="autoRefresh" <?php echo $autoRefresh ? 'checked' : ''; ?> onchange="toggleAutoRefresh(this.checked)">
                <label for="autoRefresh">自动刷新(10秒)</label>
            </div>
            <a href="view_log.php" class="btn">刷新</a>
            <a href="index.php" class="btn">返回首页</a>
            <form method="post" style="margin: 0;" onsubmit="return confirm('确定要清空日志吗？');">
                <button type="submit" name="clear_log" class="btn btn-danger">清空日志</button>
            </form>
        </div>
    </div>
    
    <div class="log-container">
        <div class="log-info">
            <div>
                <strong>日志文件:</strong> logs/app_<?php echo date('Y-m-d'); ?>.log
            </div>
            <div>
                <strong>最后更新:</strong> <?php echo date('Y-m-d H:i:s'); ?>
            </div>
        </div>
        
        <div class="log-content">
            <?php if (empty($logContent)): ?>
                <div class="empty-log">暂无日志记录</div>
            <?php else: ?>
                <?php
                $lines = explode("\n", $logContent);
                foreach ($lines as $line) {
                    if (empty(trim($line))) continue;
                    
                    $class = 'log-line';
                    if (strpos($line, '[INFO]') !== false) {
                        $class .= ' info';
                    } elseif (strpos($line, '[ERROR]') !== false) {
                        $class .= ' error';
                    } elseif (strpos($line, '[WARNING]') !== false) {
                        $class .= ' warning';
                    } elseif (strpos($line, '[DEBUG]') !== false) {
                        $class .= ' debug';
                    }
                    
                    echo '<div class="' . $class . '">' . htmlspecialchars($line) . '</div>';
                }
                ?>
            <?php endif; ?>
        </div>
    </div>
    
    <script>
        function toggleAutoRefresh(enabled) {
            if (enabled) {
                window.location.href = 'view_log.php?refresh=1';
            } else {
                window.location.href = 'view_log.php';
            }
        }
        
        // 自动滚动到底部
        window.addEventListener('load', function() {
            const logContent = document.querySelector('.log-content');
            logContent.scrollTop = logContent.scrollHeight;
        });
    </script>
</body>
</html>
